#! /bin/sh

#Goto here
here="/users/left/guillaume/bots/food"
cd $here

#Defina-se aqui a universidade 
# 1 -
# 2 -
# 3 -
# 4 -
# 5 -
# 6 -
# 7 - IST
# 8 -
u=7

#RSS feed address for EuroMillions
feed="http://www.sas.utl.pt/alim/Ementas/View/View.aspx?__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=%2FwEPDwULLTE3NDk4MzUyMzkPZBYCAgEPZBYOAgEPDxYCHgdWaXNpYmxlaGQWAgIBDzwrAAkBAA8WBB4IRGF0YUtleXMWAB4LXyFJdGVtQ291bnQCAWQWAmYPZBYMAgEPDxYCHgRUZXh0BQoyMS0wOS0yMDA3ZGQCAw8PFgIfA2VkZAIFDw8WAh8DBRBDcmVtZSBkZSBjZW5vdXJhZGQCBw8PFgIfA2VkZAIJDw8WAh8DBStGcmFuZ28gZ3Vpc2FkbyBjb20gcHVyw6kgZGUgYmF0YXRhIGUgc2FsYWRhZGQCCw8PFgIfAwUgU29saGEgZnJpdGEgY29tIGHDp29yZGEgZSBzYWxhZGFkZAIFDw8WAh8AaGQWAgIBDzwrAAkAZAIGDw8WAh8AZ2QWAgIBDzwrAAkBAA8WBB8BFgAfAgIBZBYCZg9kFhQCAQ8PFgIfAwUKMjEtMDktMjAwN2RkAgMPDxYCHwNlZGQCBQ8PFgIfA2VkZAIHDw8WAh8DZWRkAgkPDxYCHwNlZGQCCw8PFgIfA2VkZAINDw8WAh8DZWRkAg8PDxYCHwNlZGQCEQ8PFgIfA2VkZAITDw8WAh8DZWRkAg4PPCsACgEADxYCHgJTRBYBBgDYBhvxycmIZGQCEg8PFgIfAwUbSW5zdGl0dXRvIFN1cGVyaW9yIFTDqWNuaWNvZGQCEw8PFgIfAwUDSVNUZGQCFA8PFgIfAGhkFgICAQ88KwAJAGQYAQUeX19Db250cm9sc1JlcXVpcmVQb3N0QmFja0tleV9fFggFDEltYWdlQnV0dG9uOAUMSW1hZ2VCdXR0b243BQxJbWFnZUJ1dHRvbjYFDEltYWdlQnV0dG9uNQUMSW1hZ2VCdXR0b240BQxJbWFnZUJ1dHRvbjMFDEltYWdlQnV0dG9uMgUMSW1hZ2VCdXR0b24xsxImpn%2FMkBULvHiwAp2KIrjQjuM%3D&__EVENTVALIDATION=%2FwEWOgK6yJreBgLSws2HBwLSwpH2DALSwqXRBQLSwum%2FCwLSwv2aBALSwsGJCgLSwtXkAgLE3fTrCAKjy4QoAtv3taUGAtv3ocAPAtv3nXsC2%2FfJwwUC2%2Fel%2Fw4CmJSkiAsCmJSQowwC85nCqwkCmJSM3gUCmJT4%2BQ4CmJTUlAYCmJTAzw8CmJS8awKYlKiGCAKYlMTuDgKMmcKrCQKYlLCKBgLzrcKSAQLzrb7OCgLzrarpAwLzrYaECwLzrfK%2FDALzre7aBQL3mb7sCQLzrdr1DgLzrbaRBgLzreL5BALzrd6UDALuuuC9DwLuutxYAu66yPMJAvKZqqwIAu66pK8BAu66kMoKAu66jOUDAu66%2BIALAu661LsMAu66gIQBAu66%2FL8KAvWZhuwIAsnTj8gFAsnT%2B%2BMOAsnT154GAsnTw7kPAsnTv1UCydOr8AkCydOHqwEC0sKZ0whcKl3qBrRaVuS%2BZu6R8INyqy79YA%3D%3D&ImageButton$u.x=39&ImageButton$u.y=16"

#temporary file
file="sasutl"

#Weather twitter bot
twitbot=login:passw

#Read the RSS feed and filter it
curl $feed | tail -n 1 > $file.tmp

#Checks if the message is new or not by counting the number of different lines.
#If the message has unchanged then the number of lines is '0'. 
#Otherwise the number of lines is greater than zero.
if [ `diff ${file}.txt ${file}.tmp | wc -l` -gt 0 ]
then

    #Replace the message text
    cp $file.tmp $file.txt    

    #Timestamp the log file
    echo ------------------------------------------------------->> $file.log
    date >> $file.log

    #Read the forecast into a weather variable
    read mess < $file.txt
    #echo $mess

    #Twit the weather variable away
    #curl --basic --user $twitbot --data status="$mess" http://twitter.com/statuses/update.xml >> $file.log
    echo $mess

else

    #Print message
    echo ${file}.txt and ${file}.tmp are unchanged

fi
